package com.wyd.studentboot.controller;

import com.wyd.studentboot.bean.StudentBean;
import com.wyd.studentboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AddStudentController {

    @Autowired
    StudentService studentService;

    @RequestMapping("/students")
    public String showStudents(ModelMap modelMap){
        List<StudentBean> studentList = studentService.queryAllStudent();
        modelMap.addAttribute("studentList",studentList);
        return "students";
    }

    @RequestMapping("/Addstudents")
    public String showAddStudents(ModelMap modelMap){
       /* List<StudentBean> studentList = studentService.queryAllStudent();
        modelMap.addAttribute("studentList",studentList);*/
        return "Addstudents";
    }

    @RequestMapping("/addStudent")
    @ResponseBody
    public Map addStudent(StudentBean studentBean){
        int flag = studentService.addStudent(studentBean);
        Map<String,Object> map = new HashMap<String, Object>();
        if(flag == 1){
            map.put("msg","新增学生成功");
            return map;
        }else{
            map.put("msg","新增学生失败");
            return map;
        }
    }

    @RequestMapping(value = "/deleteStudent+{id}")
    public String dropStudent(@PathVariable("id")String id,ModelMap modelMap){
        int flag = studentService.dropStudent(id);
        List<StudentBean> studentList = studentService.queryAllStudent();
        modelMap.addAttribute("studentList",studentList);
        if(flag == 1){
            return "students";
        }else {
            return "error";
        }
    }

    @RequestMapping("/student+{id}")
    public String queryStudent(@PathVariable("id") String id,ModelMap modelMap){
        StudentBean studentBean = studentService.queryStudentById(id);
        modelMap.addAttribute("student_id",studentBean);
        return "studentInfo";
    }

    @RequestMapping("/modifyStudent")
    @ResponseBody
    public Map modifyStudent(StudentBean studentBean){
        int flag = studentService.modifyStudent(studentBean);
        Map<String,Object> map = new HashMap<String, Object>();
        if(flag == 1){
            map.put("msg","修改学生信息成功");
            return map;
        }else {
            map.put("msg","修改学生信息失败");
            return map;
        }
    }

    @RequestMapping("/students_findName")
    public String students_findName(ModelMap modelMap,String student_name){
        List<StudentBean> student_list =studentService.findName(student_name);
        modelMap.addAttribute("studentList",student_list);
        return "students";
    }


}
